package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;
import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseForm;
import com.maoye.ossp.sys.base.entity.SysBaseModule;
import com.maoye.ossp.sys.base.service.SysBaseFormService;
import com.maoye.ossp.sys.base.service.SysBaseModuleService;
import com.maoye.ossp.sys.site.utils.BeanToMap;
/**
 * 表单controller类
 * @author yangyy2
 * @date   2014-06-06
 */
@Controller
@RequestMapping(value = "sys/base/form/")
public class SysBaseFormController extends BaseController{
	
	/**
	 * 根节点编号
	 */
	private static final String ROOT = "1";
	/**
	 * 是否存在 (1是 0否)
	 */
	private static final String IS_RESULT_SUCCESS = "0";
	private static final String IS_RESULT_FAIL = "1";
	
	@Resource
	private SysBaseFormService sysBaseFormService ;
	
	@Resource
	private SysBaseModuleService sysBaseModuleService ;
	
	
	@RequestMapping(value = "{sysBaseModuleID}/list")
	public String list(@PathVariable Long sysBaseModuleID, HttpServletRequest request,
			HttpServletResponse response, Model model , SysBaseForm sysBaseForm) {
		sysBaseForm.setModuleId(sysBaseModuleID);
		QueryFilter queryFilter = getQuerFilter(request,sysBaseForm);
		if(!"0".equals(sysBaseModuleID)){
			queryFilter.getParams().put("moduleId", sysBaseModuleID);
			model.addAttribute("typeId", sysBaseModuleID);
		} else {
			model.addAttribute("typeId", "0");
		}
		Page<SysBaseForm> page = (Page<SysBaseForm>) sysBaseFormService.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("sysBaseForm", sysBaseForm);
		return "/sys/base/form/list";
	}
	
	@RequestMapping(value = "{sysBaseModuleID}/add")
	public String add(@PathVariable Long sysBaseModuleID,SysBaseForm sysBaseForm, Model model) {
		sysBaseForm.setId(0L);
		sysBaseForm.setModuleId(sysBaseModuleID);
		SysBaseModule module = sysBaseModuleService.get(sysBaseModuleID);
		sysBaseForm.setModuleName(module.getName());
		model.addAttribute("sysBaseForm", sysBaseForm);
		return "/sys/base/form/add";
	}
	
	@RequestMapping(value = "{sysBaseFormID}/edit")
	public String edit(@PathVariable Long sysBaseFormID, Model model) {
		SysBaseForm sysBaseForm = sysBaseFormService.get(sysBaseFormID);
		model.addAttribute("sysBaseForm", sysBaseForm);
		model.addAttribute("moduleName", sysBaseForm.getModuleName());
		return "/sys/base/form/update";
	}
	
	@RequestMapping(value = "save")
	public String save(SysBaseForm sysBaseForm,RedirectAttributes redirectAttributes,HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		SysBaseModule sysModule = sysBaseModuleService.get(sysBaseForm.getModuleId());
		sysBaseForm.setModuleName(sysModule.getName());
		
		if(sysBaseForm.getId().longValue() == 0l) {
			sysBaseForm.setId(idGenerator.getUId());
			sysBaseForm.setCreateBy("yyy");
			sysBaseFormService.saveForm(sysBaseForm);
		} else {
			sysBaseForm.setUpdateBy("yyy");
			sysBaseFormService.updateForm(sysBaseForm);
		}
		return "redirect:/sys/base/form/"+sysBaseForm.getModuleId()+"/list";
	}
	
	@RequestMapping(value = "{sysBaseFormID}/delete")
	public String delete(@PathVariable Long sysBaseFormID, Model model,
			RedirectAttributes redirectAttributes) {
		SysBaseForm form = sysBaseFormService.get(sysBaseFormID);
		String moduleId = form.getModuleId().toString();
 		sysBaseFormService.remove(sysBaseFormID);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/base/form/"+moduleId+"/list";
	}
	
	/**
	 * 验证表单是否重复
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "validate")
	public void validate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String key = request.getParameter("key");
		String id = request.getParameter("id");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		SysBaseForm sysBaseForm = new SysBaseForm();
		sysBaseForm.setKey(key);
		List<SysBaseForm> list = sysBaseFormService.getListWithEqual(BeanToMap.beanToMap(sysBaseForm));
		StringBuffer result = new StringBuffer();
		if(list.size() == 1 && list.get(0).getId().toString().equals(id)){
			result.append(IS_RESULT_SUCCESS);
		}else if(list.size() > 0){
			result.append(IS_RESULT_FAIL);
		} else {
			result.append(IS_RESULT_SUCCESS);
		}
		out.write(result.toString());
		out.flush();
		out.close();
	}
	
	
	public static void main(String args[]) {
		String sysBaseModule = "{'id':'0','name':'sss','key':'sss','orderBy':'0','remark':'sss','createBy':'sss'}";
		Gson gson = new Gson();
		SysBaseModule sysModule = gson.fromJson(sysBaseModule, SysBaseModule.class);
		System.out.println(sysModule.toString());
	}
}